using System.Collections.Generic;

namespace UFrame
{
    public class TimeObjectManager : ITimeObjectBase
    {
        List<ITimeObjectBase> dataList = new List<ITimeObjectBase>();
        protected float endTime;
        protected float startTime = 0f;
        protected float length = 0f;
        ITimeObjectBase manager = null;

        public void Add(ITimeObjectBase[] objs)
        {
            if (objs == null)
            {
                return;
            }

            foreach (ITimeLineObjectBase obj in objs)
            {
                if (obj == null)
                {
                    return;
                }

                obj.SetFather(this);
            }
            dataList.AddRange(objs);
            FindStartAndEndTime();
        }

        public void Add(ITimeObjectBase obj)
        {
            if (obj == null)
            {
                return;
            }

            obj.SetFather(this);
            dataList.Add(obj);
            FindStartAndEndTime();
        }

        public void Remove(ITimeObjectBase obj)
        {
            if (dataList.Contains(obj))
            {
                dataList.Remove(obj);
            }
        }

        public void SetFather(ITimeObjectBase obj)
        {
            manager = obj;
        }

        public void TimeHaveChange()
        {
            FindStartAndEndTime();
        }

        public void ChangeStartTime(float time)
        {
            startTime = time;
            //foreach (ITimeObjectBase obj in m_dataList)
            //{
            //    obj.ChangeStartTime(time);               
            //}
        }
        public void ChangeAllStartTime(float time)
        {
            startTime = time;
            foreach (ITimeObjectBase obj in dataList)
            {
                obj.ChangeStartTime(time);
            }
        }

        public float GetEndTime()
        {
            return startTime + length;
        }

        public float GetStartTime()
        {
            return startTime;
        }

        public virtual void TimeChange(float time)
        {
            time = time - startTime;
            foreach (ITimeObjectBase obj in dataList)
            {
                if (time <= obj.GetEndTime() && time >= obj.GetStartTime())
                {
                    obj.TimeChange(time);
                }
                else
                {
                    obj.LeaveTime(time);
                }
            }
        }

        private void FindStartAndEndTime()
        {
            //m_length = 0f;
            foreach (ITimeObjectBase obj in dataList)
            {
                //if (obj.GetStartTime()< m_startTime)
                //{
                //    m_startTime = obj.GetStartTime();
                //}
                //if (obj.GetEndTime() > m_length)
                //{
                //    m_length = obj.GetEndTime();
                //}
            }
        }

        public void LeaveTime(float time)
        {
            foreach (ITimeObjectBase obj in dataList)
            {
                obj.LeaveTime(time);
            }
        }

        public virtual void Release()
        {
            if (manager != null)
            {
                manager.Remove(this);
                manager = null;
            }
            foreach (ITimeObjectBase obj in dataList)
            {
                obj.SetFather(null);
                obj.Release();
            }
        }
    }
}